<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="返修单号" prop="repairCode">
        <el-input
          v-model="queryParams.repairCode"
          placeholder="请输入返修单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="返修类型" prop="repairType">
        <el-select v-model="queryParams.repairType" placeholder="请选择返修类型" clearable>
          <el-option
            v-for="dict in dict.type.return_repair_type.filter((item) => (item.value !== 'directly_add' && item.value !== 'abnormality_report'))"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="返修状态" prop="repairStatus">
        <el-select v-model="queryParams.repairStatus" placeholder="请选择返修单状态" clearable>
          <el-option
            v-for="dict in dict.type.return_repair_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="物料编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>
    <div style="height: 15px"></div>
    <el-card style="border-radius: 10px">
    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['rr:returnrepair:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['rr:returnrepair:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['rr:returnrepair:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['rr:returnrepair:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading" :data="returnrepairList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="返修单号" align="center" prop="repairCode" width="150">
        <template slot-scope="scope">
          <el-button
              type="text"
              @click="handleView(scope.row)"
              v-hasPermi="['rr:returnrepair:query']"
          >{{scope.row.repairCode}}</el-button>
        </template>
      </el-table-column>

      <el-table-column label="返修类型" align="center" prop="repairType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.return_repair_type" :value="scope.row.repairType"/>
        </template>
      </el-table-column>

      <el-table-column label="检验单编号" align="center" prop="qcCode" width="150"/>
      <el-table-column label="检验单关联单号" align="center" prop="qcSourceDocCode" width="150"/>
      <el-table-column label="检验单关联类型" align="center" prop="qcSourceDocType" />
      <el-table-column label="物料编码" align="center" prop="itemCode" width="150"/>
      <el-table-column label="物料名称" align="center" prop="itemName" width="150"/>
      <el-table-column label="规格型号" align="center" prop="specification" />
      <el-table-column label="单位" align="center" prop="unitOfMeasure" />
      <el-table-column label="批次号" align="center" prop="batchCode" width="150"/>
      <el-table-column label="返修数量" align="center" prop="reworkNum" />
      <el-table-column label="工作站" align="center" prop="workstationName" />
<!--      <el-table-column label="工艺路线编号" align="center" prop="routeCode" width="150"/>
      <el-table-column label="工艺路线名称" align="center" prop="routeName" width="150"/>-->
      <el-table-column label="计划完工日期" align="center" prop="plannedCompletionDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.plannedCompletionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际完工日期" align="center" prop="actualCompletionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.actualCompletionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="返修单状态" align="center" prop="repairStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.return_repair_status" :value="scope.row.repairStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template slot-scope="scope">

            <el-button
              v-if="scope.row.repairStatus == 'PREPARE'"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['rr:returnrepair:edit']"
            >修改</el-button>

            <el-button
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-if="scope.row.repairStatus == 'PREPARE'"
                v-hasPermi="['rr:returnrepair:remove']"
            >删除</el-button>

            <el-button
                type="text"
                icon="el-icon-success"
                @click="handleFinished(scope.row)"
                v-if="scope.row.repairStatus == 'RETURNING'"
                v-hasPermi="['rr:returnrepair:remove']"
            >完工</el-button>

            <el-button
                type="text"
                icon="el-icon-info"
                @click="handleView(scope.row)"
                v-if="scope.row.repairStatus == 'FINISHED'"
                v-hasPermi="['rr:returnrepair:query']"
            >详情</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>
    <!-- 添加或修改返修管理-返修工单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="返修单号" prop="repairCode" >
              <el-input v-model="form.repairCode" placeholder="请输入返修单号" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="返修类型" prop="repairType">
              <el-select v-model="form.repairType" placeholder="请选择返修类型" @change="handleRepairTypeChange" :disabled="form.repairStatus == 'RETURNING' || optType =='view'">
                <el-option
                    v-for="dict in dict.type.return_repair_type.filter((item) => (item.value !== 'directly_add' && item.value !== 'abnormality_report'))"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="返修单状态" prop="repairStatus">
              <el-select v-model="form.repairStatus" placeholder="请选择返修单状态" disabled >
                <el-option
                    v-for="dict in dict.type.return_repair_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="检验单编号" prop="qcCode">
              <el-input v-model="form.qcCode" placeholder="请输入检验单编号" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="检验单关联单号" prop="qcSourceDocCode">
              <el-input v-model="form.qcSourceDocCode" placeholder="请输入检验单关联单号" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="检验单关联类型" prop="qcSourceDocType">
              <el-input v-model="form.qcSourceDocType" placeholder="请输入检验单关联单号" disabled/>
<!--              <el-select v-model="form.qcSourceDocType" placeholder="请选择检验单关联类型">
                <el-option label="请选择字典生成" value="" />
              </el-select>-->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="物料编码" prop="itemCode">
              <el-input v-model="form.itemCode" placeholder="请输入物料编码" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="物料名称" prop="itemName">
              <el-input v-model="form.itemName" placeholder="请输入物料名称" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="规格型号" prop="specification">
              <el-input v-model="form.specification" placeholder="请输入规格型号" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="单位" prop="unitOfMeasure">
              <el-input v-model="form.unitOfMeasure" placeholder="请输入单位" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="批次号" prop="batchCode">
              <el-input v-model="form.batchCode" placeholder="请输入批次号" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="返修数量" prop="reworkNum">
              <el-input v-model="form.reworkNum" placeholder="请输入返修数量" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="计划完工日期" prop="plannedCompletionDate" >
              <el-date-picker clearable :disabled="form.repairStatus == 'RETURNING' || optType == 'view'"
                              v-model="form.plannedCompletionDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择计划完工日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工作站" prop="workstationId" v-if="workstationVisible">
<!--              <el-input v-model="form.workstationId" placeholder="请输入工作站" />-->
              <el-select v-model="form.workstationId" placeholder="请输入工作站" @change="handleChangeWorkstation" :disabled="form.repairStatus == 'RETURNING' || optType == 'view'">
                <el-option
                  v-for="item in workstationsOptions"
                  :key="item.workstationId"
                  :label="item.workstationName"
                  :value="item.workstationId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
<!--          <el-col :span="8">
            <el-form-item label="工艺路线编号" prop="routeCode" v-if="routeVisible">
              <el-input v-model="form.routeCode" placeholder="请输入工艺路线编号" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工艺路线名称" prop="routeName" v-if="routeVisible">
              <el-input v-model="form.routeName" placeholder="请输入工艺路线名称" :disabled="true"/>
            </el-form-item>
          </el-col>-->
          <el-col :span="8">
            <el-form-item label="实际完工日期" prop="actualCompletionDate" v-if="form.repairStatus == 'RETURNING'"  >
              <el-date-picker clearable :disabled="optType == 'view'"
                              v-model="form.actualCompletionDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择实际完工日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-tabs type="border-card" v-if="routeVisible">
          <el-tab-pane label="组成工序">
            <Routeprocess  :optType="optType" :repairId="form.id"></Routeprocess>
          </el-tab-pane>
        </el-tabs>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="optType == 'returning'" @click="queding">确 定</el-button>
        <el-button type="primary" v-if="optType !== 'returning' && optType !== 'view'" @click="tijiao">提 交</el-button>
        <el-button type="primary" v-if="optType !== 'returning' && optType !== 'view'" @click="submitForm()">保 存</el-button>

        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listReturnrepair,
  getReturnrepair,
  delReturnrepair,
  addReturnrepair,
  updateReturnrepair,
  getRouteInfo, finishWork
} from "@/api/mes/rr/returnrepair";
import { deleteByRepairId } from "@/api/mes/rr/retrunrepairline";

import Routeprocess from "@/views/mes/rr/returnrepair/routeprocess.vue";
import {getWsList} from "@/api/mes/md/workstation";
import {getReturn} from "@/api/mes/rr/return";

export default {
  name: "Returnrepair",
  components: {Routeprocess},
  dicts: ['return_repair_status', 'return_repair_type'],
  data() {
    return {
      disabled:false,
      routeVisible:false,
      workstationVisible:false,
      workstationsOptions:[],
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 返修管理-返修工单表格数据
      returnrepairList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        repairCode: null,        repairType: null,        repairStatus: null,        itemCode: null,        itemName: null,      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        repairType: [
          { required: true, message: "返修类型不能为空", trigger: "blur" }
        ],
        plannedCompletionDate: [
          { required: true, message: "计划完成日期不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getWorkstation()
  },
  methods: {
    /** 查询返修管理-返修工单列表 */
    getList() {
      this.loading = true;
      listReturnrepair(this.queryParams).then(response => {
        this.returnrepairList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getWorkstation(){
      //获取工作张
      getWsList().then(response => {
        this.workstationsOptions=[]
        this.workstationsOptions = response.data;
        console.log("333333",this.workstationsOptions)
      });
    },
    handleRepairTypeChange(value){
     //线上返修 需要显示工序列表
      if('online_return' == value){
        this.routeVisible= true
        this.workstationVisible= false
        //将工作站的数据清掉  主要是怕有些人  线上返修填完了相关信息，又改为线下返修了
        this.workstationId=''
        this.workstationCode=''
        this.workstationName=''
      }
      // 线下返修  选择工作站即可
      if('offline_return' == value){
        this.routeVisible= false
        this.workstationVisible=true
        //将工序的数据清掉 要是怕有些人  线上返修填完了相关信息，又改为线下返修了
        if(this.form.id){
          deleteByRepairId(this.form.id)
        }
      }

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,        repairCode: null,        repairType: null,        repairStatus: null,        qcCode: null,        qcType: null,        qcSourceDocCode: null,        qcSourceDocType: null,        itemId: null,        itemCode: null,        itemName: null,        specification: null,        unitOfMeasure: null,        batchCode: null,        reworkNum: null,        plannedCompletionDate: null,        actualCompletionDate: null,        routeCode: null,        routeName: null,        createBy: null,        createByName: null,        createTime: null,        updateBy: null,        updateTime: null,        tenantId: null      };
      this.resetForm("form");
      this.routeVisible= false
      this.workstationVisible= false
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加返修管理-返修工单";
    },
    handleChangeWorkstation(value){
      this.form.workstationCode=''
      this.form.workstationName=''
      console.log("222222",this.workstationsOptions)
      this.form.workstationCode=this.workstationsOptions.filter(item => item.workstationId === value)[0].workstationCode
      console.log("444444444",this.form.workstationCode)
      this.form.workstationName=this.workstationsOptions.filter(item => item.workstationId === value)[0].workstationName
      console.log("555555",this.form.workstationName)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.$nextTick(() => {
        getReturnrepair(id).then(response => {
          this.form = response.data;

          if('online_return' == response.data.repairType){
            this.routeVisible= true
            this.workstationVisible= false
          }else if('offline_return' == response.data.repairType){
            this.routeVisible= false
            this.workstationVisible= true
          }else{
            this.routeVisible= false
            this.workstationVisible= false
          }
          this.open = true;
          this.optType='update'
          this.title = "修改返修管理-返修工单";
        });
      })

    },
    handleView(row){
      this.reset();
      const id = row.id || this.ids
      getReturnrepair(id).then(response => {
        this.form = response.data;
        if('online_return' == response.data.repairType){
          this.routeVisible= true
          this.workstationVisible= false
        }else if('offline_return' == response.data.repairType){
          this.routeVisible= false
          this.workstationVisible= true
        }else{
          this.routeVisible= false
          this.workstationVisible= false
        }
        this.disabled=true
        this.open = true;
        this.optType='view'
        this.title = "返修工单详情";
      });

    },
    handleFinished(row){
      this.reset();
      const id = row.id || this.ids
      getReturnrepair(id).then(response => {
        this.form = response.data;
        if('online_return' == response.data.repairType){
          this.routeVisible= true
          this.workstationVisible= false
        }else if('offline_return' == response.data.repairType){
          this.routeVisible= false
          this.workstationVisible= true
        }else{
          this.routeVisible= false
          this.workstationVisible= false
        }
        this.disabled=true
        this.open = true;
        this.optType='returning'
        this.title = "返修工单完工";
      });
    },
    tijiao(){
      this.form.repairStatus="RETURNING"
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReturnrepair(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else{
            this.$modal.msgError("数据错误");
            return
          }
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReturnrepair(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReturnrepair(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    queding() {
      finishWork({'id':this.form.id,'actualCompletionDate':this.form.actualCompletionDate}).then(response => {
        if(response.code == 200){
          this.$modal.msgSuccess("该返修单已完成");
          this.open = false;
          this.getList();
        }else{
          this.$modal.msgError("失败")
        }


      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除返修管理-返修工单编号为"' + ids + '"的数据项？').then(function() {
        return delReturnrepair(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('rr/returnrepair/export', {
        ...this.queryParams
      }, `returnrepair_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
